package com.example.lib_base.view

import android.os.Bundle
import android.os.PersistableBundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding

//基础Activity
abstract class BaseActivity<VDB:ViewDataBinding> :AppCompatActivity(){
    lateinit var binding:VDB
    protected  var mSaveInstanceState:Bundle ?=null

    //参数savedInstanceState用于获取Activity的实例状态
    //场景：当Activity重新创建（屏幕旋转，配置跟新）
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //使用DataBinding设置布局
        binding= DataBindingUtil.setContentView(this,getLayoutId())
        //初始化Ui
        initViews()
    }

    //获取Activity重建后的实例状态参数
    override fun onRestoreInstanceState(
        savedInstanceState: Bundle?,
        persistentState: PersistableBundle?
    ) {
        super.onRestoreInstanceState(savedInstanceState, persistentState)

        mSaveInstanceState=savedInstanceState!!
        mSaveInstanceState?.run {
            getActivityState(this)
        }
    }

    //保存当前Activity的状态
    //屏幕旋转，配置更新
    override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
        super.onSaveInstanceState(outState, outPersistentState)

        saveActivityState(outState)
    }


    protected open abstract fun getLayoutId():Int
    protected open abstract fun initViews()
    protected open abstract fun getActivityState(state:Bundle)
    protected open abstract fun saveActivityState(outState: Bundle)

}